﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace RoadTeam_Generate.SQL
{
    public class CreateSqlModel
    {
        public static StringBuilder GetGenerateCode(string TableName, DataTable dt)
        {
            StringBuilder CreatBuild = new StringBuilder();

            CreatBuild.AppendLine("using System;");
            CreatBuild.AppendLine("using System.Collections.Generic;");
            CreatBuild.AppendLine("using System.Text;");
            CreatBuild.AppendLine("using System.ComponentModel;");
            CreatBuild.AppendLine("using RoadTeam.Model.Base;");
            CreatBuild.AppendLine("using System.Data;");
            CreatBuild.AppendLine("using RoadTeam.Enums;");
            CreatBuild.AppendLine("");
            CreatBuild.AppendLine("");
            CreatBuild.AppendLine("namespace RoadTeam.Model");
            CreatBuild.AppendLine("{");
            CreatBuild.AppendLine("    [Table(TableName = \"" + TableName + "\")]");
            CreatBuild.AppendLine("    public class " + TableName + " : iTableEntityBase");
            CreatBuild.AppendLine("    {");
            CreatBuild.AppendLine("");
            CreatBuild.AppendLine("        public " + TableName + "()");
            CreatBuild.AppendLine("        {");
            CreatBuild.AppendLine("            TableName = @\"" + TableName + "\";");
            CreatBuild.AppendLine("            EntityMap = Db_DataType.Table;");
            CreatBuild.AppendLine("        }");
            CreatBuild.AppendLine("");
            CreatBuild.AppendLine("        /// <summary>");
            CreatBuild.AppendLine("        /// 所有字段名");
            CreatBuild.AppendLine("        /// </summary>");
            CreatBuild.AppendLine("        protected override void SetFieldNames()");
            CreatBuild.AppendLine("        {");
            String Temp = String.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                Temp += "\"" + dr["字段名"] + "\",";
            }
            CreatBuild.AppendLine("            PropertyNames = new string[] { " + Temp.TrimEnd(',') + "};");
            CreatBuild.AppendLine("        }");
            CreatBuild.AppendLine("");
            CreatBuild.AppendLine("");
            CreatBuild.AppendLine("        #region 属性");
            String IsPrimaryKey = bool.FalseString;
            String Identity = bool.FalseString;
            String CanBeNull = bool.FalseString;
            foreach (DataRow dr in dt.Rows)
            {
                IsPrimaryKey = (!String.IsNullOrEmpty(dr["主键"].ToString()) && dr["主键"].ToString() == "是") ? bool.TrueString : bool.FalseString;
                Identity = (!String.IsNullOrEmpty(dr["标识"].ToString()) && dr["标识"].ToString() == "是") ? bool.TrueString : bool.FalseString;
                CanBeNull = (!String.IsNullOrEmpty(dr["允许空"].ToString()) && dr["允许空"].ToString() == "是") ? bool.TrueString : bool.FalseString;
                CreatBuild.AppendLine("        /// <summary>");
                CreatBuild.AppendLine("        ///" + dr["说明"] + "");
                CreatBuild.AppendLine("        /// </summary>");
                CreatBuild.AppendLine("        [Column(ColumnName = \"" + dr["字段名"] + "\", IsPrimaryKey = " + IsPrimaryKey.ToLower() + @",Identity = " + Identity.ToLower() + ", DbType = \"" + dr["类型"] + "\", Length = " + dr["长度"] + ", CanBeNull = " + CanBeNull.ToLower() + @",Description ="+"\""+ dr["说明"] +"\""+"),DefaultValue(\"" + dr["默认值"] + "\")]");
                CreatBuild.AppendLine("        public " + GetDataType(dr["类型"], dr["允许空"]) + " " + dr["字段名"] + "");
                CreatBuild.AppendLine("        {");
                CreatBuild.AppendLine("            get { return getProperty<" + GetDataType(dr["类型"], dr["允许空"]) + ">(\"" + dr["字段名"] + "\"); }");
                CreatBuild.AppendLine("            set { setProperty(\"" + dr["字段名"] + "\", value); }");
                CreatBuild.AppendLine("        }");
            }
            CreatBuild.AppendLine("        #endregion");
            CreatBuild.AppendLine("        #region 插入方法");
            CreatBuild.AppendLine("        /// <summary>");
            CreatBuild.AppendLine("        /// 对象插入数据库");
            CreatBuild.AppendLine("        /// </summary>");
            CreatBuild.AppendLine("        /// <param name=\"O\">插入的对象</param>");
            CreatBuild.AppendLine("        /// <param name=\"IExcept\">剔除不需要插入的列</param>");
            CreatBuild.AppendLine("        /// <param name=\"Con\">连接字符串</param>");
            CreatBuild.AppendLine("        /// <returns>是否插入成功</returns>");
            CreatBuild.AppendLine("        public static bool InserObjedct("+TableName+" O, List<String> IExcept, String Con)");
            CreatBuild.AppendLine("        {");
            CreatBuild.AppendLine("            return TbEvlutionText.InserObjedct(typeof(" + TableName + "), O, IExcept,Con);");
            CreatBuild.AppendLine("        }");
            CreatBuild.AppendLine("");
            CreatBuild.AppendLine("        /// <summary>");
            CreatBuild.AppendLine("        /// 对象插入数据库");
            CreatBuild.AppendLine("        /// </summary>");
            CreatBuild.AppendLine("        /// <param name=\"O\">插入的对象</param>");
            CreatBuild.AppendLine("        /// <param name=\"Con\">连接字符串</param>");
            CreatBuild.AppendLine("        /// <returns>是否插入成功</returns>");
            CreatBuild.AppendLine("        public static bool InserObjedct(" + TableName + " O, String Con)");
            CreatBuild.AppendLine("        {");
            CreatBuild.AppendLine("            return TbEvlutionText.InserObjedct(typeof(" + TableName + "), O, null,Con);");
            CreatBuild.AppendLine("        }");
            CreatBuild.AppendLine("");
            CreatBuild.AppendLine("        /// <summary>");
            CreatBuild.AppendLine("        ///  数据集插入到数据库表");
            CreatBuild.AppendLine("        ///  暂不支持回写自增长ID");
            CreatBuild.AppendLine("        /// </summary>");
            CreatBuild.AppendLine("        /// <param name=\"dt\">数据集</param>");
            CreatBuild.AppendLine("        /// <param name=\"Con\">连接字符串</param>");
            CreatBuild.AppendLine("        /// <returns></returns>");
            CreatBuild.AppendLine("        public static bool InsertDatatable(DataTable dt, String Con)");
            CreatBuild.AppendLine("        {");
            CreatBuild.AppendLine("            return TbEvlutionText.InsertDatatable(typeof(" + TableName + "), dt,Con);");
            CreatBuild.AppendLine("        }");
            CreatBuild.AppendLine("         //TODO:这里添加其他插入方法");
            CreatBuild.AppendLine("        #endregion");
            CreatBuild.AppendLine("");
            CreatBuild.AppendLine("        #region 查找方法");
            CreatBuild.AppendLine("        /// <summary>");
            CreatBuild.AppendLine("        /// 查找数据集合");
            CreatBuild.AppendLine("        /// </summary>");
            CreatBuild.AppendLine("        /// <param name=\"where\">条件对象</param>");
            CreatBuild.AppendLine("        /// <param name=\"Con\">连接字符串</param>");
            CreatBuild.AppendLine("        /// <returns>数据集合</returns>");
            CreatBuild.AppendLine("        public static DataTable Select(SqlWhere where,String Con)");
            CreatBuild.AppendLine("        {");
            CreatBuild.AppendLine("            return TbEvlutionText.Select(typeof(" + TableName + "), where,Con);");
            CreatBuild.AppendLine("        }");
            CreatBuild.AppendLine("         //TODO:这里添加其他查找方法");
            CreatBuild.AppendLine("        #endregion");
            CreatBuild.AppendLine("");
            CreatBuild.AppendLine("        #region 删除方法");
            CreatBuild.AppendLine("        /// <summary>");
            CreatBuild.AppendLine("        /// 删除指定数据");
            CreatBuild.AppendLine("        /// </summary>");
            CreatBuild.AppendLine("        /// <param name=\"where\">条件集合</param>");
            CreatBuild.AppendLine("        /// <param name=\"Con\">连接字符串</param>");
            CreatBuild.AppendLine("        /// <returns>是否删除成功</returns>");
            CreatBuild.AppendLine("        public static bool Delete(OQLWhere where, String Con)");
            CreatBuild.AppendLine("        {");
            CreatBuild.AppendLine("            return TbEvlutionText.Delete(typeof(" + TableName + "), where,Con) > 0;");
            CreatBuild.AppendLine("        }");
            CreatBuild.AppendLine("         //TODO:这里添加其他删除方法");
            CreatBuild.AppendLine("        #endregion");

            CreatBuild.AppendLine("        #region 修改方法");


            CreatBuild.AppendLine("        /// <summary>");
            CreatBuild.AppendLine("        /// 对象更新某指定行数值");
            CreatBuild.AppendLine("        /// </summary>");
            CreatBuild.AppendLine("        /// <param name=\"O\">对象</param>");
            CreatBuild.AppendLine("        /// <param name=\"Con\">连接字符串</param>");
            CreatBuild.AppendLine("        /// <returns></returns>");
            CreatBuild.AppendLine("        public static bool Update("+TableName+" O,String Con)");
            CreatBuild.AppendLine("        {");
            CreatBuild.AppendLine("            return " + TableName + ".Update(typeof(" + TableName + "), O, Con) > 0;");
            CreatBuild.AppendLine("        }");
            CreatBuild.AppendLine("");
            CreatBuild.AppendLine("        /// <summary>");
            CreatBuild.AppendLine("        ///   数据集合更新数据库");
            CreatBuild.AppendLine("        ///   数据集合中的列明必须要和数据库中的列明一一对应");
            CreatBuild.AppendLine("        /// </summary>");
            CreatBuild.AppendLine("        /// <param name=\"dt\">数据集</param>");
            CreatBuild.AppendLine("        /// <param name=\"Con\">连接字符串</param>");
            CreatBuild.AppendLine("        /// <returns></returns>");
            CreatBuild.AppendLine("        public static bool Update(DataTable dt, String Con)");
            CreatBuild.AppendLine("        {");
            CreatBuild.AppendLine("            return " + TableName + ".Update(typeof(" + TableName + "), dt,  Con) > 0;");
            CreatBuild.AppendLine("        }");

            CreatBuild.AppendLine("        //TODO:这里添加其他修改方法");
            CreatBuild.AppendLine("        #endregion");

            CreatBuild.AppendLine("    }");
            CreatBuild.AppendLine("}");
            return CreatBuild;
        }

        public static String GetDataType(object type, object Isnull)
        {
            String StrType = (type == null) ? "" : type.ToString();
            String StrIsnull = (Isnull == null) ? "" : Isnull.ToString();
            String StrReturnValue="string";
            switch (StrType)
            {
                    
                case "bit":
                    StrReturnValue = "bool";
                    break;
                case "datetime":
                    StrReturnValue = "DateTime";
                    break;
                case "decimal":
                    StrReturnValue = "decimal";
                    break;
                case "text": 
                case "varchar":
                default:
                    StrReturnValue = "string";
                    break;
            }
            if (!String.IsNullOrEmpty(StrIsnull) && StrIsnull == "是" && StrReturnValue != "string")
                StrReturnValue += "?";
            return StrReturnValue;
        }
    }
}
